package com.hmyd.hmyd_android.adapter;

import java.util.ArrayList;
import java.util.List;

import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.Investment;
import com.hmyd.hmyd_android.ui.InvestmentDetailActivity;
import com.hmyd.hmyd_android.ui.TicketsDetailsActivity;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.waveview.WaveHelper;
import com.hmyd.hmyd_android.view.waveview.WaveView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewPagerAdapter extends PagerAdapter {
	private Context context;
	private List<View> list;  
	private List<Investment> investments;

	public ViewPagerAdapter(Context context, List<Investment> investments) {
		this.context = context;
		this.investments = investments;
		list = getViewPager(investments);
	}

	@Override
	public int getCount() {
		return list != null ? list.size() : 0;
	}

	// 是否是同一张图片
	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public void destroyItem(ViewGroup view, int position, Object object) {
		if (list == null && list.size() > 0) {
			return;
		}

		((ViewPager) view).removeView(list.get(position));
	}

	@Override
	public Object instantiateItem(ViewGroup view, final int position) {
		if (list == null && list.size() > 0) {
			return view;
		}

		((ViewPager) view).addView(list.get(position));

		list.get(position).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (v.getTag().equals("grpj")) {
					context.startActivity(TicketsDetailsActivity.getIntent(
							context, investments.get(position)));
				} else {
					context.startActivity(InvestmentDetailActivity.getIntent(
							context, investments.get(position)));
				}
			}
		});

		return list.get(position);
	}

	/**
	 * 获取ViewPager内容
	 * @param list
	 * @return
	 */
	private List<View> getViewPager(List<Investment> list){
		List<View> views = new ArrayList<View>();
		ViewHolder holder = new ViewHolder();

		for (Investment investment : list) {
			View view = null;
			if (investment.type.equals("grpj")) {
				view = LayoutInflater.from(context).inflate(R.layout.view_recommend_viewpager_bill, null);
				view.setTag(investment.type);

				holder.view = view;

				holder.title = (TextView) view.findViewById(R.id.title);
				holder.activity_tag = (ImageView) view.findViewById(R.id.activity_tag);
				holder.rate = (TextView) view.findViewById(R.id.rate);
				holder.rate_sp = (TextView) view.findViewById(R.id.rate_sp);

				holder.limit = (TextView) view.findViewById(R.id.limit);

				holder.investInfo = (TextView) view.findViewById(R.id.investInfo);
				holder.smartWay = (TextView) view.findViewById(R.id.smartWay);
				holder.repayMethod = (TextView) view.findViewById(R.id.repayMethod);

				if (investment.period != 7) {
					LayoutParams params = holder.limit.getLayoutParams();
					holder.limit.setVisibility(View.INVISIBLE);
					params.height = 1;
					
					holder.limit.setLayoutParams(params);
				} else {
					holder.limit.setVisibility(View.VISIBLE);
				}

				// 设置票据图标
				setBillIcon(investment, holder);
				// 设置票据文字
				setBillText(investment, holder);

			} else {
				view = LayoutInflater.from(context).inflate(R.layout.view_recommend_viewpager, null);
				view.setTag(investment.type);

				holder.view = view;

				holder.title = (TextView) view.findViewById(R.id.title);
				holder.activity_tag = (ImageView) view.findViewById(R.id.activity_tag);

				holder.type = (ImageView) view.findViewById(R.id.type);
				holder.waveMan = (WaveView) view.findViewById(R.id.waveMan);
				holder.rate = (TextView) view.findViewById(R.id.rate);
				holder.rate_sp = (TextView) view.findViewById(R.id.rate_sp);
				holder.minInvestAmount = (TextView) view.findViewById(R.id.minInvestAmount);
				holder.period = (TextView) view.findViewById(R.id.period);

				if (investment.type.equals("shf")) {
					holder.type.setImageResource(R.drawable.tou_lei_shou);
				} else if (investment.type.equals("phb")) {
					holder.type.setImageResource(R.drawable.tou_lei_pu);
				} else if (investment.type.equals("hht")) {
					holder.type.setImageResource(R.drawable.tou_lei_hong);
				} else {
					holder.type.setVisibility(View.GONE);
				}

				// 起投金额
				String recomment_amount = context.getString(R.string.recomment_amount);
				String msg_amount = String.format(
						recomment_amount, 
						investment.minInvestAmount
						);

				holder.minInvestAmount.setText(changeTextColor(msg_amount, String.valueOf(investment.minInvestAmount)));

				// 投资期限
				String recomment_period = context.getString(R.string.recomment_period);
				String msg_period = String.format(
						recomment_period, 
						investment.period
						);

				msg_period = msg_period + Utils.periodConversion(investment.periodUnit);
				holder.period.setText(changeTextColor(msg_period, String.valueOf(investment.period)));

				// 设置水波进度
				setWave(holder, investment.progress);

			}

			// 获取标题
			holder.title.setText(investment.title);
			// 获取标签图像
			if (TextUtils.isEmpty(investment.itemtagIcon)) {
				holder.activity_tag.setVisibility(View.GONE);
			} else {
				AppBasic.getBitmapUtils(context).display(holder.activity_tag,
						investment.itemtagIcon);
			}

			// 获取利率
			getRateView(investment, holder);

			views.add(view);
		}

		return views;
	}

	/**
	 * 设置票据文字颜色
	 * @param investment
	 * @param holder
	 */
	private void setBillText(Investment investment, ViewHolder holder){
		Utils.changeTextSize(investment.investInfo, holder.investInfo, context.getResources()
				.getColor(R.color.text_orange));
		Utils.changeTextSize(investment.smartWay, holder.smartWay, context.getResources()
				.getColor(R.color.text_orange));
		Utils.changeTextSize(investment.repayMethod, holder.repayMethod, context.getResources()
				.getColor(R.color.text_orange));
	}

	/**
	 * 设置票据图标
	 * @param investment
	 * @param holder
	 */
	private void setBillIcon(Investment investment, ViewHolder holder){
		if (investment.period == 1) {
			holder.investInfo.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ind_icon_1yin, 0, 0);
			holder.smartWay.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ind_icon_2huo, 0, 0);
			holder.repayMethod.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ind_icon_3xi, 0, 0);
		}  else if (investment.period == 7) {
			holder.investInfo.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ind_icon_1yin, 0, 0);
			holder.smartWay.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ind_icon_2xin, 0, 0);
			holder.repayMethod.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ind_icon_3gao, 0, 0);
		} else {
			holder.investInfo.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ind_icon_1yin, 0, 0);
			holder.smartWay.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ind_icon_2suo, 0, 0);
			holder.repayMethod.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ind_icon_3mei, 0, 0);
		}
	}


	private int mBorderWidth = 3;
	public WaveHelper mHelper;
	/**
	 * 设置水波样式
	 * @param holder
	 * @param progress
	 */
	private void setWave(ViewHolder holder, String progress){
		holder.waveMan.setShapeType(WaveView.ShapeType.CIRCLE);
		// 设置水波圆边框宽度及颜色
		holder.waveMan.setBorder(Utils.dip2px(context, mBorderWidth), 
				context.getResources().getColor(R.color.wave_border_color));

		holder.waveMan.setWaveColor(Color.parseColor("#00000000"),
				Color.parseColor("#88fee8c1"));

		float waveLevel = 0f;
		if (!TextUtils.isEmpty(progress)) {
			waveLevel = Float.parseFloat(progress) / 100;
		}

		mHelper = new WaveHelper(holder.waveMan, waveLevel);
		mHelper.start();

		View root = ((Activity) context).getWindow().getDecorView();

		int vp_height = root.findViewById(R.id.vp_recommend).getHeight();
		int vp_width = Utils.screenWidth_;

		int wave_height = ((vp_height - vp_width) > 0) ? vp_width : vp_height;

		// 除去正下方小圆点高度、标题高度
		wave_height = wave_height - root.findViewById(R.id.dotcontent).getHeight() - Utils.dip2px(context, 20)
				- holder.title.getHeight() - Utils.dip2px(context, 10);

		// 设置布局参数
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.waveMan
				.getLayoutParams();

		lp.width = wave_height;
		lp.height = wave_height;
		holder.waveMan.setLayoutParams(lp);
	}

	/**
	 * 改变部分文本内容
	 * @param msg 文本内容
	 * @param str 要改变的文本
	 */
	private SpannableStringBuilder changeTextColor(String msg, String str){
		SpannableStringBuilder style = new SpannableStringBuilder(msg);
		style.setSpan(
				new ForegroundColorSpan(context.getResources().getColor(R.color.text_orange)), 
				msg.indexOf(str), 
				msg.indexOf(str) + str.length(), 
				Spannable.SPAN_EXCLUSIVE_INCLUSIVE
				);

		return style;
	}

	/**
	 * 获取利率
	 * @param investment
	 * @param holder
	 */
	private void getRateView(Investment investment, ViewHolder holder){
		String rate_value = investment.rate;
		String rate = "";
		String rate_sp = "";

		// 如果包含%， 删除所有%  手动添加
		rate_value = rate_value.contains("%") ? rate_value.replace("%", "") : rate_value;

		if (rate_value.contains("+")) {
			rate = rate_value.split("[+]")[0];
			rate_sp = rate_value.split("[+]")[1];

			holder.rate.setText(rate);
			holder.rate_sp.setText("% + "+rate_sp+"%");
		} else {
			holder.rate_sp.setVisibility(View.GONE);
			
			holder.rate.setText(rate_value + "%");
		}
	}

	static class ViewHolder{
		View view;

		// 标题
		TextView title;
		// 活动标签
		ImageView activity_tag;
		// 利率数值
		TextView rate;
		TextView rate_sp;

		// 定期标
		ImageView type;
		WaveView waveMan;
		TextView minInvestAmount;
		TextView period;

		// 票据
		// 限投提示信息
		TextView limit;
		// 底部提示信息
		TextView investInfo;
		TextView smartWay;
		TextView repayMethod;
	}
}